package com.lushuan.ch06_springboot_web_rest.entity;

import org.springframework.http.HttpStatus;

public class ResponseMessage<T> {
    private Integer code;   // 200 500 404
    private String message; // 请求成功
    private T data;  // 泛型接受不同类型

    public ResponseMessage() {

    }

    public ResponseMessage(Integer code, String message, T data) {
        this.code = code;
        this.message = message;
        this.data = data;
    }

    // 避免每次controller 返回的时候都要创建 ResponseMessage 实例
    public static <T>  ResponseMessage<T> success(T data) {
        ResponseMessage responseMessage = new ResponseMessage();
        responseMessage.setCode(HttpStatus.OK.value());  // 指定一个枚举 200
        responseMessage.setMessage("success");
        responseMessage.setData(data);
        return responseMessage;

    }

    public static <T>  ResponseMessage<T> success() {
        ResponseMessage responseMessage = new ResponseMessage();
        responseMessage.setCode(HttpStatus.OK.value());  // 指定一个枚举 200
        responseMessage.setMessage("success");
        return responseMessage;

    }

    public Integer getCode() {
        return code;
    }

    public void setCode(Integer code) {
        this.code = code;
    }

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    public T getData() {
        return data;
    }

    public void setData(T data) {
        this.data = data;
    }


}
